package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.mapper.CategoryMapper;
import com.ruoyi.system.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int createCategory(Category category) {
        category.setCategoryName(category.getCategoryName());
        category.setCategoryDetails(category.getCategoryDetails());
        category.setCreateTime(DateUtils.getNowDate());
        int rows = categoryMapper.saveCategory(category);
        return rows;
    }

    /**
     * 获取分类列表
     * @param category
     * @return
     */
    @Override
    public List<Category> getCategoryList(Category category) {
        List<Category> categoryList = categoryMapper.getCategoryList(category);
        return categoryList;
    }

    /**
     * 搜索分类
     * @param category
     * @return
     */
    @Override
    public List<Category> searchCategory(Category category) {
        List<Category> categoryList = categoryMapper.searchCategory(category);
        return categoryList;
    }

    /**
     * 删除分类
     * @param category
     * @return
     */
    @Override
    public int deleteCategory(Category category) {
        category.setRemoveTime(DateUtils.getNowDate());
        category.setRemoveStatus("1");
        int rows = categoryMapper.removeCategory(category);
        return rows;
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @Override
    public int updateCategory(Category category) {
        category.setUpdateTime(DateUtils.getNowDate());
        int rows = categoryMapper.updateCategory(category);
        return rows;
    }

    /**
     * 获取分类信息
     * @param id
     * @return
     */
    @Override
    public Category getCategory(Integer id) {
        Category category = categoryMapper.getCategory(id);
        return category;
    }

    /**
     * 获取分类id
     * @return
     */
    @Override
    public List<Integer> getCategoryIds() {
        List<Integer> categoryIds = categoryMapper.getCategoryIds();
        return categoryIds;
    }


    /**
     * 获取分类名称
     * @param categoryName
     * @return
     */
    @Override
    public List<String> getCategoryName(String categoryName) {
        List<String> categoryNameArray = categoryMapper.getCategoryName(categoryName);
        return categoryNameArray;
    }

    @Override
    public Integer getCategoryIdByCategoryName(String categoryName) {
        return categoryMapper.findCategoryIdByCategoryName(categoryName);
    }

}
